package com.songshu.e.dubbo.api.graphql;

import com.songshu.e.dubbo.api.service.AdService;
import com.songshu.e.dubbo.api.vo.ad.index.IndexAdResult;
import com.songshu.e.dubbo.api.vo.ad.index.IndexAdResultData;
import com.songshu.e.dubbo.server.pojo.Ad;
import com.songshu.e.dubbo.server.vo.PageInfo;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class IndexAdDataFetcher implements MyDataFetcher{

    @Autowired
    private AdService adService;

    @Override
    public String fileName() {
        return "IndexAdList";
    }

    @Override
    public Object dataFetcher(DataFetchingEnvironment environment) {
        PageInfo<Ad> pageInfo = adService.queryAdList(1, 1, 3);
        List<Ad> ads = pageInfo.getRecords();
        List<IndexAdResultData> list = new ArrayList<>();
        ads.forEach(ad->list.add(new IndexAdResultData(ad.getUrl())));
        return new IndexAdResult(list);
    }
}
